﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public static class ProfilerNodeViewTool
{
    //public struct DebugMerge
    //{
    //    public List<Rect> rect;
    //    public ArrayRange range;
    //    public int indexOfOutArray;
    //    public int step;
    //    public int i;
    //    public bool vaild;
    //}

    public static void SimplifyMemorySectionData(MemoryHeapUsage heapData, out List<Rect> out_SimplifiedRects, int index, Rect rect)
    {
        if (heapData == null || heapData.sectors.Count == 0)
        {
            out_SimplifiedRects = new List<Rect>();
        }
        //List<DebugMerge> debugRect = new List<DebugMerge>();
        var totalRects = new List<Rect>();
        try
        {
            out_SimplifiedRects = new List<Rect>();
            for (int r = 0; r < heapData.sectors.Count; r++)
            {
                float bpw = ((float)heapData.section.bytes.Length) / (rect.width - 20);
                float x = rect.x + ((heapData.sectors[r].address - heapData.section.startAddress) / bpw);
                float y = 22 + 20 + (22 * index) + (index * 20);
                float width = heapData.sectors[r].size / bpw;
                totalRects.Add(new Rect(x, y, width, 15));
                //out_SimplifiedRects.Add(totalRects[r]);
            }
            //return;
            int step = 1;
            for (int i = 0; i < totalRects.Count; i += step)
            {

                int temp = i;
                while (temp < totalRects.Count - 1 && CloseEngouh(totalRects[temp].x + totalRects[temp].width, totalRects[temp + 1].x))
                {
                    temp++;
                }
                step = (temp - i) + 1;
                //DebugMerge merge = new DebugMerge();
                //merge.i = i;
                //merge.step = step;
                //merge.range = new ArrayRange();
                //merge.range.startIndex = i;
                //merge.range.endIndex = temp;
                //merge.indexOfOutArray = i;
                //merge.rect = new List<Rect>();
                if (step > 1)
                {
                    float width = totalRects[temp].x + totalRects[temp].width - totalRects[i].x;
                    out_SimplifiedRects.Add(new Rect(totalRects[i].x, totalRects[i].y, Mathf.Abs(width), 15));
                    //double segma = 0;
                    //for (int x = i; x <= temp; x++)
                    //{
                    //    segma += totalRects[x].width;
                    //    merge.rect.Add(totalRects[x]);
                    //}
                    //merge.vaild = segma <= width;
                }
                else
                {
                    out_SimplifiedRects.Add(new Rect(totalRects[i].x, totalRects[i].y, totalRects[i].width, 15));
                    //merge.vaild = true;
                    //merge.rect.Add(totalRects[i]);
                }
                //debugRect.Add(merge);
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e);
            out_SimplifiedRects = new List<Rect>();
            return;
        }
        //bool allVaild = true;
        //foreach (var item in debugRect)
        //{
        //    if (!item.vaild)
        //    {
        //        allVaild = false;
        //        break;
        //    }
        //}
        //Debug.Log("Simplified :" + out_SimplifiedRects.Count + " Original: " + heapData.sectors.Count);
    }


    public static bool CloseEngouh(float pX1, float pX2)
    {
        //return false;
        return Mathf.Abs(pX1 - pX2) < 2f;
    }
}

